/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.ui.detail;

import com.siro.pm.I18nManager;
import com.siro.pm.model.Developer;
import com.siro.pm.model.provider.DeveloperProvider;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.GridLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.PasswordField;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since ResetPwdPage.java v1.0
 */
public class ResetPwdPage extends VerticalLayout {
	private static final long serialVersionUID = 6225106255250310462L;

	private Window window;

	private TextField name;
	private PasswordField password;
	private PasswordField password2;
	private Button button_Save;

	public ResetPwdPage() {
	}

	public void createContents() {
		setMargin(true);
		setSpacing(true);

		// base info
		GridLayout developerBaseInfo = new GridLayout();
		developerBaseInfo.setStyleName("h2");
		developerBaseInfo.setCaption(I18nManager.getMessage("password.change"));
		developerBaseInfo.setColumns(1);
		developerBaseInfo.setMargin(true);
		developerBaseInfo.setSpacing(true);
		developerBaseInfo.setWidth("100%");

		name = new TextField(DeveloperProvider.DEVELOPER_PROPERTY_NAME);
		name.setWidth("100%");
		developerBaseInfo.addComponent(name);

		password = new PasswordField(
				DeveloperProvider.DEVELOPER_PROPERTY_PASSWORD);
		password.setWidth("100%");
		developerBaseInfo.addComponent(password);

		password2 = new PasswordField(
				I18nManager.getMessage("password.change.input.required"));
		password2.setWidth("100%");
		developerBaseInfo.addComponent(password2);

		// buttons
		HorizontalLayout buttons = new HorizontalLayout();
		buttons.setMargin(true);
		buttons.setSpacing(true);
		buttons.setWidth("100%");

		button_Save = new Button(I18nManager.getMessage("button.save"));
		button_Save.setWidth(50, Sizeable.UNITS_PIXELS);
		button_Save.addListener(new SaveClickListener());
		buttons.addComponent(button_Save);
		buttons.setComponentAlignment(button_Save, Alignment.MIDDLE_RIGHT);

		addComponent(developerBaseInfo);
		addComponent(buttons);

	}

	class SaveClickListener implements ClickListener {
		private static final long serialVersionUID = -2851840644753744125L;

		public void buttonClick(ClickEvent event) {
			String pwd1 = (String) password.getValue();
			String pwd2 = (String) password2.getValue();
			if (pwd1 != null && pwd2 != null && pwd1.equals(pwd2)) {
				Developer developer = Developer.getDeveloper((String) name
						.getValue());
				developer.setPassword(pwd2);
				Developer.updateDeveloper(developer);
			} else {
				getWindow().showNotification(
						I18nManager.getMessage("password.change.input.match"));
			}
			if (window != null) {
				(window.getParent()).removeWindow(window);
			}
		}
	}

	public Window getResetPwdWindow() {
		return window;
	}

	public void setWindow(Window window) {
		this.window = window;
	}

}
